package Model.DAO;

import Model.Beans.Institution;
import Model.Beans.Student;
import Model.Utilities.Constantes.StudentState;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Restrictions;

public class StudentDAO  extends AbstracDAO<Student>{
    public List<Student> getAllStudents(Institution institution) {
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria studentCri = session.createCriteria(Student.class);
        
        studentCri.setFetchMode("user", FetchMode.JOIN);
        studentCri.setFetchMode("institution", FetchMode.JOIN);
        studentCri.add(Restrictions.eq("institution.idinstitution", institution.getIdinstitution()));
        
        Criteria userCri = studentCri.createCriteria("user");
        userCri.add(Restrictions.eq("state", StudentState.APROBADO));
        
        List<Student> students = studentCri.list();
        session.close();
        return students;
    }
    
    public List<Student> buscarEstudiante(String userId) {
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria studentCri = session.createCriteria(Student.class);
        
        studentCri.setFetchMode("user", FetchMode.JOIN);
        
        Criteria userCri = studentCri.createCriteria("user");
        userCri.add(Restrictions.eq("iduser", Integer.parseInt(userId)));
        
        List<Student> students = studentCri.list();
        session.close();
        return students;
    }
    
    
    public Student searchById(int idStudent){
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Student.class);
        criteria.add(Restrictions.eq("idstudent", idStudent));
        Student student = (Student) criteria.uniqueResult();
        session.close();
        return student;
    }
    
    public void updateAprobado(int idStudent){
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Student.class);
        session.close();  
    }
}
